package com.dhcy.mqtt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhcy.mqtt.dao.AutoMapper;
import com.dhcy.mqtt.par.ht.AutoPar;
import com.dhcy.mqtt.par.ht.PagePar;
import com.dhcy.mqtt.result.ht.TableResult;
import com.dhcy.mqtt.result.ht.TableResultData;
import com.dhcy.mqtt.service.AutoCodeService;
import com.dhcy.mqtt.util.AppResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("auto")
@Slf4j
public class AutoController {

    @Autowired
    AutoMapper autoMapper;

    @Autowired
    AutoCodeService autoCodeService;

    @Value("${spring.datasource.url}")
    private  String url;



    @GetMapping("tableList.json")
    TableResultData tableList(PagePar pagePar){
        String[] urls=url.split("\\/");
         String databaseName=urls[3];
         log.info(databaseName.indexOf("?")+"");
         databaseName=databaseName.substring(0,databaseName.indexOf("?"));
         log.info("当前数据库名称"+databaseName);
         IPage page=new Page<>(pagePar.getPage(),pagePar.getPageSize());
        List<TableResult> tableResults = autoMapper.tableList(page, databaseName);
        return TableResultData.success(page.getTotal(),tableResults);
    }

    @PostMapping("autoCode.json")
    AppResultData autoCode(AutoPar autoCodePar){
        String[] urls=url.split("\\/");
        String databaseName=urls[3];
        log.info(databaseName.indexOf("?")+"");
        databaseName=databaseName.substring(0,databaseName.indexOf("?"));
        autoCodeService.autoCode(autoCodePar,databaseName);
        return  AppResultData.successMessage("生成成功");
    }


    //此方法备用
    AppResultData fieldList(String tableName){
        return  null;
    }
}
